/**   
 * @Title: BaseDaoImpl.java 
 * @Package com.leesche.zcsp.center.web.dao.impl 
 * @Description:() 
 * @author dennies yang
 * @date 2016年9月21日 下午10:19:01 
 * @version V1.0   
 */
package com.leesche.zcsp.node.web.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.leesche.zcsp.node.utils.web.PagingCondition;
import com.leesche.zcsp.node.web.dao.BaseDao;

/**
 * @Title: BaseDaoImpl.java
 * @Package com.leesche.zcsp.center.web.dao.impl
 * @Description:()
 * @author dennies yang
 * @date 2016年9月21日 下午10:19:01
 * @version V1.0
 */
public abstract class BaseDaoImpl<T, S, D extends Serializable, P extends PagingCondition, ID extends Serializable>
		extends SqlSessionDaoSupport implements BaseDao<T, S, D, P, ID> {
	
	private static final String DB_COUNTBYEXAMPLE="countByExample";
	private static final String DB_DELETEBYEXAMPLE="deleteByExample";
	private static final String DB_DELETEBYPRIMARYKEY="deleteByPrimaryKey";
	private static final String DB_INSERT="insert";
	private static final String DB_INSERTSELECTIVE="insertSelective";
	private static final String DB_SELECTBYEXAMPLE="selectByExample";
	private static final String DB_UPDATEBYEXAMPLESELECTIVE="updateByExampleSelective";
	private static final String DB_UPDATEBYEXAMPLE="updateByExample";
	private static final String DB_UPDATEBYPRIMARYKEYSELECTIVE="updateByPrimaryKeySelective";
	private static final String DB_UPDATEBYPRIMARYKEY="updateByPrimaryKey";
	private static final String DB_SELECTPAGEBYCRITERIA="selectPageByCriteria";
	private static final String DB_COUNTPAGEBYCRITERIA="countPageByCriteria";
	

	@Override
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}

	@Override
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		super.setSqlSessionTemplate(sqlSessionTemplate);
	}

	/***
	 * 
	 * @Title: getStatement
	 * @Description: TODO()
	 * @param @param sqlId
	 * @param @return
	 * @return String
	 * @throws
	 */
	public String getStatement(String sqlId) {
		// 决定了mapper.xml namespace中的值
		String name = this.getClass().getName();
		StringBuilder sb = new StringBuilder();
		sb.append(name).append(".").append(sqlId);
		return sb.toString();
	}

	@Override
	public long countByExample(S s) {
		// TODO Auto-generated method stub
		return getSqlSession()
				.selectOne(this.getStatement(DB_COUNTBYEXAMPLE), s);

	}

	@Override
	public int deleteByExample(S s) {
		// TODO Auto-generated method stub
		return getSqlSession().delete(this.getStatement(DB_DELETEBYEXAMPLE), s);
	}

	@Override
	public int deleteByPrimaryKey(ID id) {
		// TODO Auto-generated method stub
		return getSqlSession().delete(this.getStatement(DB_DELETEBYPRIMARYKEY),
				id);
	}

	@Override
	public int insert(T t) {
		// TODO Auto-generated method stub
		return this.getSqlSession().insert(
				this.getStatement(DB_INSERT), t);
	}

	@Override
	public int insertSelective(T t) {
		// TODO Auto-generated method stub
		return this.getSqlSession().insert(
				this.getStatement(DB_INSERTSELECTIVE), t);

	}

	@Override
	public List<Map<String, Object>> selectByExample(S s) {
		// TODO Auto-generated method stub
		return this.getSqlSession().selectList(
				this.getStatement(DB_SELECTBYEXAMPLE), s);
	}

	@Override
	public T selectByPrimaryKey(ID id) {
		// TODO Auto-generated method stub
		return this.getSqlSession().selectOne(
				this.getStatement(DB_SELECTBYEXAMPLE), id);
	}

	@Override
	public int updateByExampleSelective(T t, S s) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("record", t);
		map.put("criteria", s);
		return this.getSqlSession().update(
				this.getStatement(DB_UPDATEBYEXAMPLESELECTIVE), map);
	}

	@Override
	public int updateByExample(T t, S s) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("record", t);
		map.put("criteria", s);

		return this.getSqlSession().update(
				this.getStatement(DB_UPDATEBYEXAMPLE), map);

	}

	@Override
	public int updateByPrimaryKeySelective(T t) {
		// TODO Auto-generated method stub
		return this.getSqlSession().update(
				this.getStatement(DB_UPDATEBYPRIMARYKEYSELECTIVE), t);
	}

	@Override
	public int updateByPrimaryKey(T t) {
		// TODO Auto-generated method stub
		return this.getSqlSession().update(
				this.getStatement(DB_UPDATEBYPRIMARYKEY), t);

	}

	@Override
	public List<D> selectPageByCriteria(P p) {
		// TODO Auto-generated method stub
		return this.getSqlSession().selectList(
				this.getStatement(DB_SELECTPAGEBYCRITERIA), p);
	}

	@Override
	public Integer countPageByCriteria(P p) {
		// TODO Auto-generated method stub
		return this.getSqlSession().selectOne(
				this.getStatement(DB_COUNTPAGEBYCRITERIA), p);
	}

	@Override
	public D selectByCondition(P p) {
		return null;
	}

	@Override
	public List<D> selectListsByCondition(P p) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	

}
